package com.starbucks.opencase.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.data.daPath;
import com.starbucks.opencase.data.daScan;
import com.starbucks.opencase.entity.entProperties;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMMark;
import com.starbucks.opencase.enums.enumCMTip;
import com.starbucks.opencase.function.funCarry;
import com.starbucks.opencase.function.funSession;
import com.starbucks.opencase.modualr.moPetCarry;
import com.starbucks.opencase.modualr.moPetInfo;
import com.starbucks.opencase.tool.toolProperties;
import com.starbucks.opencase.tool.toolUserAccount;
import com.starbucks.opencase.tool.toolUserGoods;
import com.starbucks.opencase.tool.toolUserPet;
import org.apache.tomcat.util.digester.DocumentProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class serUserBattle {
    @Autowired
    private funSession funSession;
    @Autowired
    private funCarry funCarry;
    @Autowired
    private toolUserPet toolUserPet;
    @Autowired
    private toolUserAccount toolUserAccount;
    @Autowired
    private toolUserGoods toolUserGoods;
    @Autowired
    private toolProperties toolProperties;
    /**
     * 匹配对手
     * @param id
     * @param message
     */
    public void matching(String id, JsonNode message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.mark.name(), enumCMMark.battleMatching.ordinal());
        on.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
        on.put(enumCMKey.status.name(), false);
        if(!funSession.isOnlineOfId(id,on))return;
        if(!message.has(enumCMKey.pid.name())){
            on.put(enumCMKey.tip.name(), enumCMTip.no_has_pid.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        String pid=message.get(enumCMKey.pid.name()).asText();
        String uid= funSession.getUIdOfId(id);
        if(funCarry.isCarry(uid,pid)){
            on.put(enumCMKey.tip.name(), enumCMTip.not_pet_leisure.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        moPetCarry carry=funCarry.randomGet(uid);
        boolean is_robot=false;
        moPetInfo rival;
        if(carry==null){
//            on.put(enumCMKey.tip.name(), enumCMTip.not_find_rival.getTip());
//            funSession.sendMessage(id,on);
            rival=toolUserPet.queryInfo(toolUserPet.randomPID());
            is_robot=true;
        }else{
            rival=toolUserPet.queryInfo(carry.getPid());
        }
        moPetInfo self=toolUserPet.queryInfo(pid);
        if(self==null || rival==null){
            on.put(enumCMKey.tip.name(), enumCMTip.not_find_rival.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        self.setUid(uid);
        self.setUname(toolUserAccount.queryNickname(uid));
        self.setUurl(daPath.getAvatar(toolUserAccount.queryGender(uid)+""));
//        self.setPid(pid);
        self.setPname(toolUserPet.queryName(pid));
        self.setPurl(daPath.getPet(pid));
        self.setAnimationJson(daPath.getAnimationJson(pid));
        self.setAnimationAtlas(daPath.getAnimationAtlas(pid));
        self.setAnimationPng(daPath.getAnimationPng(pid));
        rival.setUid(is_robot?"robot":carry.getUid());
        rival.setUname(is_robot?"神秘人":toolUserAccount.queryNickname(carry.getUid()));
        rival.setUurl(daPath.getAvatar(is_robot?"-1":toolUserAccount.queryGender(carry.getUid())+""));
//        rival.setPid(carry.getPid());
        rival.setPname(toolUserPet.queryName(rival.getPid()));
        rival.setUurl(daPath.getPet(rival.getPid()));
        rival.setAnimationJson(daPath.getAnimationJson(rival.getPid()));
        rival.setAnimationAtlas(daPath.getAnimationAtlas(rival.getPid()));
        rival.setAnimationPng(daPath.getAnimationPng(rival.getPid()));
        on.put(enumCMKey.bid.name(),is_robot?0:carry.getStart());
        on.put(enumCMKey.self.name(), om.valueToTree(self));
        on.put(enumCMKey.rival.name(), om.valueToTree(rival));
        on.put(enumCMKey.status.name(), true);
        if(is_robot){funCarry.addRobot(uid);//添加机器人
        }else funCarry.pauseCarry(carry.getStart());//暂停运输
        funSession.sendMessage(id,on);
    }

    /**
     * 结束战斗
     * @param id
     * @param message
     */
    public void end(String id,JsonNode message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.mark.name(), enumCMMark.battleEnd.ordinal());
        on.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
        on.put(enumCMKey.status.name(), false);
        if(!funSession.isOnlineOfId(id,on))return;
        if(!message.has(enumCMKey.bid.name())){
            on.put(enumCMKey.tip.name(), enumCMTip.no_has_id.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        long bid=Long.parseLong(message.get(enumCMKey.bid.name()).asText());
        String uid= funSession.getUIdOfId(id);
        if(bid<=0)funCarry.removeRobot(uid);
        if(!message.has(enumCMKey.result.name())){
            on.put(enumCMKey.tip.name(), enumCMTip.no_has_result.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        boolean result=message.get(enumCMKey.result.name()).asBoolean();
        if(!result){//失败
            if(bid>0)funCarry.startCarry(bid);//恢复运输
            on.put(enumCMKey.tip.name(), enumCMTip.go_on_carry.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        //成功
        if(bid>0){
            funCarry.stopCarry(bid);//结束运输
            moPetCarry carry=funCarry.query(bid);
            toolUserGoods.addGoods(uid, carry.getGid(), 1);
            String gname=toolUserGoods.queryName(carry.getGid());
            //对手被劫通知
            if(funSession.isOnlineOfUId(carry.getUid())){//对手在线
                ObjectNode onn=om.createObjectNode();
                onn.put(enumCMKey.mark.name(), enumCMMark.battleEnd.ordinal());
                onn.put(enumCMKey.status.name(), true);
                onn.put(enumCMKey.pid.name(), carry.getPid());
                onn.put(enumCMKey.pname.name(), toolUserPet.queryName(carry.getPid()));
                onn.put(enumCMKey.purl.name(), daPath.getPet(carry.getPid()));
                onn.put(enumCMKey.gid.name(),carry.getGid());
                onn.put(enumCMKey.gname.name(), gname);
                onn.put(enumCMKey.gurl.name(), daPath.getGoods(carry.getGid()));
                onn.put(enumCMKey.tip.name(), enumCMTip.no_has_result.getTip());
                onn.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
                funSession.sendMessage(funSession.getIdOfUId(carry.getUid()), onn);
            }else{//对手不在线
                daScan.lose.put(bid,carry);
            }
            on.put(enumCMKey.gid.name(),carry.getGid());
            on.put(enumCMKey.gname.name(), gname);
            on.put(enumCMKey.gurl.name(), daPath.getGoods(carry.getGid()));
            on.put(enumCMKey.type.name(), 1);
        }else{
            entProperties ent=toolProperties.queryInfo();
            on.put(enumCMKey.energy.name(), ent.getRobotRewardEnergy());
            on.put(enumCMKey.type.name(), 0);
        }
        on.put(enumCMKey.tip.name(), enumCMTip.success_battle.getTip());
        on.put(enumCMKey.status.name(), true);
        funSession.sendMessage(id,on);
    }
}
